/**
 * 
 */
package UsedCar;

import org.ahmadsoft.arrays.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import uchicago.src.sim.util.Random;

/**
 * @author fychao
 * 
 */
public class Forum {
	/**
	 * @param current_id
	 *            現在加入的 id
	 */
	private int cur_id = 0;

	public static final boolean FLAG = Model.FLAG;

	/**
	 * @param maxYr
	 *            文章最大年限
	 */
	private int maxYr = 5;

	/**
	 * @param posts
	 *            所有的文章
	 */
	public FasterArrayList<Post> posts = new FasterArrayList<Post>();

	/**
	 * @param total_knows
	 *            目前論壇的總知識量
	 */
	@SuppressWarnings("unused")
	private int total_knows = 0;

	/**
	 * @param yr
	 *            Forum 的年限
	 */
	private int yr = 0;
	/**
	 * @param yr2p
	 *            年對文章的排列
	 */
	private Hashtable<Integer, List<Post>> yr2p = new Hashtable<Integer, List<Post>>();

	private int yrc = 0;

	/**
	 * 建構子
	 */
	public Forum() {
		// 產生亂數種子
		long s = System.currentTimeMillis();
		Random.setSeed(s);

		this.tick();
	}

	/**
	 * 增加一篇文章
	 * 
	 * @param post
	 *            文章內容
	 */
	public void addPost(Post post) {
		long time = System.currentTimeMillis();

		post.setPost_id(cur_id);
		post.insertYr = yr;
		post.beforeYr = yr;
		posts.add(post);

		List<Post> ps = new ArrayList<Post>();
		// put into circular_q;
		if (yr2p.containsKey(this.yrc)) {
			ps = yr2p.get(this.yrc);
		}
		ps.add(post);
		yr2p.put(this.yrc, ps);

		cur_id += 1;
		if (FLAG) {
			time = System.currentTimeMillis() - time;
			if (time > 5)
				System.out.println("Forum::addPost(): " + time);
		}
	}

	/**
	 * 讀多少文章
	 * 
	 * @param readNum
	 *            文章數
	 * @return 文章
	 */
	public List<Post> getReadPosts(int readNum) {
		long time = System.currentTimeMillis();
		ArrayList<Post> rPosts = new ArrayList<Post>();

		int size = this.posts.size();

		if (size <= 0) {
			return new ArrayList<Post>();
		}

		if (size <= readNum) {
			return this.posts;
		} else {
			Random.createUniform();
			while (rPosts.size() < readNum) {
				int item = Random.uniform.nextIntFromTo(0, size - 1);
				Post picked_Post = this.posts.get(item);

				if (!rPosts.contains(picked_Post)) {
					rPosts.add(picked_Post);
				}
			}
		}
		if (FLAG) {
			time = System.currentTimeMillis() - time;
			if (time > 5)
				System.out.println("\t !! \t getReadPosts exec time:" + time
						+ " POST SIZE:" + this.posts.size());
		}

		return rPosts;
	}

	public float getTickKnowRatio() {
		return 0.1f;
	}

	/**
	 * 列出所有文章
	 */
	public void listPost() {
		int knowsNum = 0;
		Iterator<Post> i = this.posts.iterator();
		while (i.hasNext()) {
			Post p = i.next();

			knowsNum += p.getPostKnows().length;

		}

		System.out.println("Post Num:" + this.posts.size() + " AVG KNOWS="
				+ (double) (knowsNum / (double) this.posts.size()));

	}

	public void listPostByYr() {
		int sum = 0;
		Enumeration<Integer> pk = this.yr2p.keys();
		String foo = "cyr=" + this.yrc;
		while (pk.hasMoreElements()) {
			int myr = pk.nextElement();
			List<Post> ps = this.yr2p.get(myr);
			sum += ps.size();
			if (this.yrc == myr) {
				foo += "\t[" + myr + "=" + ps.size() + "]";
			} else {
				foo += "\t" + myr + " = " + ps.size();
			}
		}
		System.out.println(foo + "\t sum - posts size()= "
				+ (sum - this.posts.size()));
	}

	/**
	 * 1. 先在 resetHash 裡紀錄 2. 將該文章掉到 今年
	 * 
	 * @param p
	 */
	public void resetPost(Post p) {

		if (p.insertYr != yr) {
			long time = System.currentTimeMillis();

			p.resetYr(this.yr);

			// 先移
			List<Post> ps = new ArrayList<Post>();
			if (yr2p.containsKey(this.yrc)) {
				ps = yr2p.get(this.yrc);
			}
			ps.add(p);
			yr2p.put(this.yrc, ps);

			// 再刪
			int b4_yrc = (p.beforeYr % this.maxYr);
			ps = yr2p.get(b4_yrc);
			ps.remove(p);
			yr2p.put(b4_yrc, ps);

			if (FLAG) {
				time = System.currentTimeMillis() - time;
				if (time > 5)
					System.out.println("Yr:" + yr + " Forum::resetPost()"
							+ time + " POST SIZE:" + ps.size() + " total:"
							+ this.posts.size());
			}
		}

	}

	/**
	 * 把年限與環型佇列的年加1
	 */
	public void tick() {
		this.yr += 1;
		this.yrc = (this.yr % this.maxYr);
	}

	/**
	 * 將所有文章加上年限
	 */
	public void tickPosts() {

		// listPostByYr();
		// 清掉新的位置裡的文章
		this.tick();
		if (this.yr > this.maxYr) {
			long time = System.currentTimeMillis();
			this.posts.removeAll((List<Post>) yr2p.get(this.yrc));
			if (FLAG) {
				time = System.currentTimeMillis() - time;

				System.out.println("(Form) in thickPosts() - removeAll(): "
						+ time);
			}
			this.yr2p.remove(this.yrc);

		}

	}

	public int getMaxYr() {
		return maxYr;
	}

	public void setMaxYr(int maxYr) {
		this.maxYr = maxYr;
	}
}
